package com.cqupt.tr.login.service;

import com.cqupt.tr.bean.AdminAuth;
import com.cqupt.tr.bean.JwtUser;
import com.cqupt.tr.bean.MonitorUser;
import com.cqupt.tr.login.mapper.LoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        MonitorUser monitorUserByname = loginMapper.getMonitorUserByname(s);
        if (monitorUserByname.getRole().equals("ROLE_ADMIN")){
            monitorUserByname.setMonitorAuth((AdminAuth)monitorUserByname.getMonitorAuth());
        }

        return new JwtUser(monitorUserByname);
    }
}
